﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

namespace MotelServices
{
    public class TaiKhoanBUS
    {
        
        public static string _connString = string.Empty;

        #region Static Contructor
        static TaiKhoanBUS()
        {
            _connString = ConfigurationManager.ConnectionStrings["MotelConnectionString"].ConnectionString.ToString();
        } 
        #endregion

        #region insert Account
        public static bool InsertAcc(string ten,string hinh)
        {
            bool result = true;
            DataMotelDataContext dbContext = new DataMotelDataContext(_connString);
            try
            {
                int rs = dbContext.spInsertAccount(ten,hinh);
                if (rs == -1)
                {
                    throw new Exception("Insert not successfully");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dbContext.Dispose();
            }
            return result;
        }
        #endregion

        #region check Account
        public static bool CheckAcc(string ten)
        {
            bool result = true;
            DataMotelDataContext dbContext = new DataMotelDataContext(_connString);
            try
            {
                var taikhoan = (from tk in dbContext.TaiKhoans
                                 where tk.TenTaiKhoan.Equals(ten)
                                 select tk);
                foreach (var item in taikhoan)
                {
                    result = false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dbContext.Dispose();
            }
            return result;
        }
        #endregion

        #region lay mataikhoan theo tentaikhoan
        public static int LayMaTaiKhoan(string ten)
        {
            int result = 0;
            DataMotelDataContext dbContext = new DataMotelDataContext(_connString);
            try
            {
                var taikhoan = (from tk in dbContext.TaiKhoans
                                where tk.TenTaiKhoan.Equals(ten)
                                select tk);
                foreach (var item in taikhoan)
                {
                    result = item.MaTaiKhoan;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dbContext.Dispose();
            }
            return result;
        }
        #endregion

        #region lay tentaikhoan theo mataikhoan
        public static string LayTenTaiKhoan(int ma)
        {
            string result = "";
            DataMotelDataContext dbContext = new DataMotelDataContext(_connString);
            try
            {
                var taikhoan = (from tk in dbContext.TaiKhoans
                                where tk.MaTaiKhoan.Equals(ma)
                                select tk);
                foreach (var item in taikhoan)
                {
                    result = item.TenTaiKhoan;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                dbContext.Dispose();
            }
            return result;
        }
        #endregion

        //Dang lam do
        //#region search ban be (chua ket ban)
        //public static int LayMaTaiKhoan(string ten)
        //{
        //    int result = 0;
        //    DataMotelDataContext dbContext = new DataMotelDataContext(_connString);
        //    try
        //    {
        //        var taikhoan = (from tk in dbContext.TaiKhoans
        //                        where tk.TenTaiKhoan.Equals(ten)
        //                        select tk);
        //        foreach (var item in taikhoan)
        //        {
        //            result = item.MaTaiKhoan;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception(ex.Message);
        //    }
        //    finally
        //    {
        //        dbContext.Dispose();
        //    }
        //    return result;
        //}
        //#endregion
    }
}